module.exports = function($scope, $resource, obj, $modalInstance, toaster, updateAddress){

    function init(){
        $scope.obj = {
            'mail_name': obj.mail_name,
            'mail_mobile': obj.mail_mobile,
            'mail_address': obj.mail_address,
            'courier_company_code': obj.courier_company_code,
            'express_number': obj.express_number,
            'entity_supplier': obj.entity_supplier,
            'entity_supplier_phone': obj.entity_supplier_phone
        };
        $scope.updateAddress = updateAddress;

        $resource('/api/as/mc/express/getExpressForConList', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
                console.log(res.data);
                $scope.expresslist = res.data;
            } else {
                toaster.error({title: '', body: '发货失败，请重试'});
            }
        });
    }
    init();


    $scope.save = function(){
        if(!$scope.obj.courier_company_code){
            toaster.warning({title: '', body: '快递公司编号不能为空'});
            return;
        }
        if(!$scope.obj.express_number){
            toaster.warning({title: '', body: '快递单号不能为空'});
            return;
        }
        var para = {
            'order_code': obj.code,
            'mail_state': '2',
            'courier_company_code': $scope.obj.courier_company_code,
            'express_number': $scope.obj.express_number
        }
        $resource('/api/ac/tc/ticketOrderService/setExpressNumber', {}, {}).save(para, function(res){
            if (res.errcode === 0 && res.data == '1') {
                toaster.success({title: '', body: '发货成功'});
                $modalInstance.close(para);
            } else {
                toaster.error({title: '', body: '发货失败，请重试'});
            }
        });
    }

    $scope.saveAddress = function(){
        if(!$scope.obj.mail_name){
            toaster.warning({title: '', body: '收货人姓名不能为空'});
            return;
        }
        if(!$scope.obj.mail_mobile || !(/^1[3|4|5|7|8|9]\d{9}$/.test($scope.obj.mail_mobile))){
            toaster.warning({title: '', body: '手机号格式不正确'});
            return;
        }
        if(!$scope.obj.mail_address){
            toaster.warning({title: '', body: '收货地址不能为空'});
            return;
        }
        var para = {
            'order_code': obj.code,
            'mail_name': $scope.obj.mail_name,
            'mail_mobile': $scope.obj.mail_mobile,
            'mail_address': $scope.obj.mail_address
        }

        $resource('/api/ac/tc/ticketOrderService/updateOrderAddress', {}, {}).save(para, function(res){
            if (res.errcode === 0 && res.data == '1') {
                toaster.success({title: '', body: '更改成功'});
                $modalInstance.close(para);
            } else {
                toaster.error({title: '', body: '更改失败，请重试'});
            }
        });
    }

    $scope.cancel = function () {
    	$modalInstance.close();
    }

};